package com.ym.manager.sys.config;

import com.ym.datasource.DataSourceType;
import com.ym.datasource.UseDataSource;
import com.ym.manager.sys.config.model.SysConfigInfo;
import com.ym.utils.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: dhf
 * @ClassName: configService
 * @Date: 2023/2/16 11:04
 * @Description:
 * @Version: 1.0
 */
@Service
public class SystemConfigService {
    @Autowired
    private SysConfigsDao sysConfigsDao;
    @UseDataSource(DataSourceType.YS_IM)
    public Pages<SysConfigInfo> getList(int pageNum, int limit, String configKey, String remark, int type) {
        Map<String,Object> map = new HashMap<>();
        int begin = pageNum;
        map.put("begin",begin);
        map.put("limit",limit);
        map.put("configKey",configKey);
        map.put("remark",remark);
        int count  = 0;
        List<SysConfigInfo> list = new ArrayList<>();
        if(type==1){
            count = sysConfigsDao.getSysListCount(map);
            list = sysConfigsDao.getSysList(map);
        }
        if(type==2){
            count = sysConfigsDao.getWalletListCount(map);
            list = sysConfigsDao.getWalletList(map);
        }
        Pages<SysConfigInfo> pages = new Pages<>(count,pageNum==0?1:pageNum/limit,limit);
        pages.setList(list);
        return pages;
    }
    @UseDataSource(DataSourceType.YS_IM)
    public int update(String configKey, String configValue, int type) {
        if(type==1){
            sysConfigsDao.updateSys(configKey,configValue);
        }
        if(type==2){
            sysConfigsDao.updateWallet(configKey,configValue);
        }
        return 1;
    }
    @UseDataSource(DataSourceType.YS_IM)
    public int del(String configKey, int type) {
        if(type==1){
            sysConfigsDao.delSys(configKey);
        }
        if(type==2){
            sysConfigsDao.delWallet(configKey);
        }
        return 1;
    }
}
